/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MantPlantillaPapelTrabajo.java
 *
 * Created on 12/09/2011, 03:49:06 PM
 */

package GUI.AdministracionPlantillas;

import BE.PlantillaMarcoReferencia;
import BE.PlantillaPapelTrabajo;
import BL.PlantillaMarcoReferenciaBl;
import BL.PlantillaPapelTrabajoBl;
import GUI.AdministracionUsuarios.UtilUsuarios;
import GUI.Principal;
import GUI.Utils.Utils;
import java.awt.Color;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author CarLita
 */
public class CreacionPlantillaMarcoReferencia extends javax.swing.JPanel {
    Principal ventanaPrincipal;
    PlantillaMarcoReferencia plantillaNueva;
    PlantillaMarcoReferenciaBl plantillaMRBl;
    PlantillaPapelTrabajoBl papelTrabajoBl;
    public int modoVentana;
    /** Creates new form MantPlantillaPapelTrabajo */
    public CreacionPlantillaMarcoReferencia() {
        initComponents();
    }

    public CreacionPlantillaMarcoReferencia(Principal ventanaPrincipal) {
        initComponents();
        this.ventanaPrincipal = ventanaPrincipal;
        plantillaMRBl = new PlantillaMarcoReferenciaBl();
        papelTrabajoBl = new PlantillaPapelTrabajoBl();
        setearModo(Principal.MODO_CREACION);
        llenarCmbTipo();
        ocultarIdTabla();
        bloquearPorPermisos();
    }

   public CreacionPlantillaMarcoReferencia(Principal ventanaPrincipal, PlantillaMarcoReferencia plantilla, int modo) {
        initComponents();
        this.ventanaPrincipal = ventanaPrincipal;
        plantillaNueva = plantilla;
        plantillaMRBl = new PlantillaMarcoReferenciaBl();
        setearModo(modo);        
        llenarCmbTipo();
        ocultarIdTabla();        
    }

    public void setearModo(int modo){
        modoVentana = modo;
        switch(modoVentana){
            case Principal.MODO_EDICION:
                btnEditar.setVisible(false);
                btnLimpiar.setVisible(false);
                btnGuardar.setVisible(true);
                llenarDatosPlantilla();
                bloquearCampos(false);
                bloquearPestaña(false);
                buscarPapelesTrabajo();
                revisarNivelesComp();
                break;
            case Principal.MODO_CREACION:
                btnEditar.setVisible(false);
                btnLimpiar.setVisible(true);
                btnGuardar.setVisible(true);
                bloquearPestaña(true);
                bloquearCampos(false);
                break;
            case Principal.MODO_LECTURA:
                btnLimpiar.setVisible(false);
                btnGuardar.setVisible(false);
                btnEditar.setVisible(true);
                llenarDatosPlantilla();
                bloquearCampos(true);
                bloquearPestaña(false);
                buscarPapelesTrabajo();
                revisarNivelesComp();
        }
    }

    private void bloquearPestaña(boolean b) {
        tabPapelesTrabajo.setEnabledAt(tabPapelesTrabajo.getTabCount()-1, !b);                
    }

    private void llenarDatosPlantilla(){
        txtNombre.setText(plantillaNueva.getNombre());
        txtMarcoReferencia.setText(plantillaNueva.getNombreMarcoReferencia());
        txtAreaEspecializacion.setText(plantillaNueva.getAreaEspecializacion());
        txtObjetivo.setText(plantillaNueva.getObjetivo());
        txtNivel1.setText(plantillaNueva.getNivel1Composicion());
        lblNivel1.setText(plantillaNueva.getNivel1Composicion()+ ":");
        txtNivel2.setText(plantillaNueva.getNivel2Composicion());
        lblNivel2.setText(plantillaNueva.getNivel2Composicion()+ ":");
        txtNivel3.setText(plantillaNueva.getNivel3Composicion());
        lblNivel3.setText(plantillaNueva.getNivel3Composicion()+ ":");
        txtDescripcion.setText(plantillaNueva.getDescripcion());
    }

    private void bloquearCampos(boolean bloquear) {
        boolean desbloqueado = !bloquear;        
        btnNuevoPT.setVisible(desbloqueado);
        btnAgregarPT.setVisible(desbloqueado);
        btnEditarPT.setVisible(desbloqueado);
        btnEliminarPT.setVisible(desbloqueado);
        txtNombre.setEditable(desbloqueado);
        txtMarcoReferencia.setEditable(desbloqueado);
        txtAreaEspecializacion.setEditable(desbloqueado);
        txtObjetivo.setEditable(desbloqueado);
        txtNivel1.setEditable(desbloqueado);
        txtNivel2.setEditable(desbloqueado);
        txtNivel3.setEditable(desbloqueado);
        txtDescripcion.setEditable(desbloqueado);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabPapelesTrabajo = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        txtMarcoReferencia = new javax.swing.JTextField();
        txtAreaEspecializacion = new javax.swing.JTextField();
        txtObjetivo = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescripcion = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtNivel1 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtNivel2 = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtNivel3 = new javax.swing.JTextField();
        lblComposicion = new javax.swing.JLabel();
        btnLimpiar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnGuardar = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        panelPapelesTrabajo = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        lblNivel1 = new javax.swing.JLabel();
        lblNivel2 = new javax.swing.JLabel();
        txtPlantilla = new javax.swing.JTextField();
        txtNivel1PT = new javax.swing.JTextField();
        txtNivel2PT = new javax.swing.JTextField();
        btnBuscar = new javax.swing.JButton();
        btnNuevoPT = new javax.swing.JButton();
        lblNivel3 = new javax.swing.JLabel();
        txtNivel3PT = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        cmbTipo = new javax.swing.JComboBox();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblPT = new javax.swing.JTable();
        btnAgregarPT = new javax.swing.JButton();
        btnEditarPT = new javax.swing.JButton();
        btnEliminarPT = new javax.swing.JButton();
        btnRefrescar = new javax.swing.JButton();
        btnLimpiar1 = new javax.swing.JButton();

        jPanel1.setName("Principal"); // NOI18N

        jLabel1.setText("Nombre (*):");

        jLabel2.setText("Marco de Referencia:");

        jLabel3.setText("Área de Especialización:");

        jLabel5.setText("Objetivo:");

        jLabel6.setText("Descripción:");

        txtDescripcion.setColumns(20);
        txtDescripcion.setFont(new java.awt.Font("Tahoma", 0, 11));
        txtDescripcion.setRows(5);
        jScrollPane1.setViewportView(txtDescripcion);

        jLabel7.setText("Composición");

        jLabel8.setText("Nivel 1 (*):");

        jLabel9.setText("Nivel 2:");

        jLabel10.setText("Nivel 3:");

        lblComposicion.setText("<html> La composición de la plantilla muestra la estructura del Marco de Referencia. Por ejemplo, COBIT tiene: Dominios (Nivel 1), Procesos (Nivel 2) y Objetivos de Control (Nivel 3)");

        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/limpiar.png"))); // NOI18N
        btnLimpiar.setText("Limpiar");
        btnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/cancelar.png"))); // NOI18N
        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/guardar.png"))); // NOI18N
        btnGuardar.setText("Guardar");
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/edit.gif"))); // NOI18N
        btnEditar.setText("Editar");
        btnEditar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(412, 412, 412)
                        .addComponent(jLabel6))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtMarcoReferencia, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtAreaEspecializacion, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtObjetivo, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(21, 21, 21)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 333, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel7))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9)
                            .addComponent(jLabel10))
                        .addGap(24, 24, 24)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtNivel1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtNivel2, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(68, 68, 68)
                                .addComponent(lblComposicion, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txtNivel3, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(26, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addComponent(jLabel6)
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel1)
                        .addGap(24, 24, 24)
                        .addComponent(jLabel2)
                        .addGap(24, 24, 24)
                        .addComponent(jLabel3)
                        .addGap(24, 24, 24)
                        .addComponent(jLabel5))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtMarcoReferencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtAreaEspecializacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(txtObjetivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(42, 42, 42)
                .addComponent(jLabel7)
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNivel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNivel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9))
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNivel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addComponent(lblComposicion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(85, 85, 85)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        tabPapelesTrabajo.addTab("Principal", jPanel1);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Búsqueda"));

        jLabel12.setText("Nombre:");

        lblNivel1.setText("{Nivel1}:");

        lblNivel2.setText("{Nivel2}:");

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/find.png"))); // NOI18N
        btnBuscar.setText("Buscar");
        btnBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        btnNuevoPT.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/new doc.png"))); // NOI18N
        btnNuevoPT.setText("<html>Nuevo Papel de Trabajo");
        btnNuevoPT.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnNuevoPT.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnNuevoPT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoPTActionPerformed(evt);
            }
        });

        lblNivel3.setText("{Nivel3}:");

        jLabel13.setText("Tipo:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(jLabel12, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                            .addComponent(lblNivel1, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(lblNivel2, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                        .addGroup(jPanel3Layout.createSequentialGroup()
                            .addComponent(lblNivel3, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cmbTipo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtNivel3PT)
                    .addComponent(txtNivel2PT)
                    .addComponent(txtPlantilla, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                    .addComponent(txtNivel1PT))
                .addGap(46, 46, 46)
                .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(36, 36, 36)
                .addComponent(btnNuevoPT, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPlantilla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtNivel1PT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblNivel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNivel2)
                            .addComponent(txtNivel2PT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblNivel3)
                            .addComponent(txtNivel3PT, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel13)
                            .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(btnNuevoPT, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnBuscar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 77, Short.MAX_VALUE)))
                .addContainerGap(11, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tblPT.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Id", "Nombre", "Nivel 1", "Nivel 2", "Nivel 3"
            }
        ));
        ocultarIdTabla();
        tblPT.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblPTMousePressed(evt);
            }
        });
        jScrollPane2.setViewportView(tblPT);

        btnAgregarPT.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/new doc.png"))); // NOI18N
        btnAgregarPT.setText("Agregar");
        btnAgregarPT.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAgregarPT.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnAgregarPT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarPTActionPerformed(evt);
            }
        });

        btnEditarPT.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/edit.gif"))); // NOI18N
        btnEditarPT.setText("Editar");
        btnEditarPT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditarPTActionPerformed(evt);
            }
        });

        btnEliminarPT.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/minus white.png"))); // NOI18N
        btnEliminarPT.setText("Eliminar");
        btnEliminarPT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarPTActionPerformed(evt);
            }
        });

        btnRefrescar.setText("Refrescar");
        btnRefrescar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefrescarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 603, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnRefrescar, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                    .addComponent(btnEliminarPT, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                    .addComponent(btnEditarPT, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                    .addComponent(btnAgregarPT, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
                .addGap(29, 29, 29))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(btnAgregarPT, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnEditarPT)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEliminarPT)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnRefrescar)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnLimpiar1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/AdministracionPlantillas/limpiar.png"))); // NOI18N
        btnLimpiar1.setText("Limpiar");
        btnLimpiar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimpiar1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout panelPapelesTrabajoLayout = new javax.swing.GroupLayout(panelPapelesTrabajo);
        panelPapelesTrabajo.setLayout(panelPapelesTrabajoLayout);
        panelPapelesTrabajoLayout.setHorizontalGroup(
            panelPapelesTrabajoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelPapelesTrabajoLayout.createSequentialGroup()
                .addGap(296, 296, 296)
                .addComponent(btnLimpiar1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(342, Short.MAX_VALUE))
            .addGroup(panelPapelesTrabajoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelPapelesTrabajoLayout.createSequentialGroup()
                    .addGroup(panelPapelesTrabajoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(panelPapelesTrabajoLayout.createSequentialGroup()
                            .addGap(0, 0, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGroup(panelPapelesTrabajoLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap()))
        );
        panelPapelesTrabajoLayout.setVerticalGroup(
            panelPapelesTrabajoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelPapelesTrabajoLayout.createSequentialGroup()
                .addContainerGap(425, Short.MAX_VALUE)
                .addComponent(btnLimpiar1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(panelPapelesTrabajoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(panelPapelesTrabajoLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(53, Short.MAX_VALUE)))
        );

        tabPapelesTrabajo.addTab("Papeles de Trabajo", panelPapelesTrabajo);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(tabPapelesTrabajo, javax.swing.GroupLayout.PREFERRED_SIZE, 776, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabPapelesTrabajo, javax.swing.GroupLayout.DEFAULT_SIZE, 495, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiarActionPerformed
        limpiar();
}//GEN-LAST:event_btnLimpiarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        mostrarMantenimientoPlantillas();
}//GEN-LAST:event_btnCancelarActionPerformed

    private void btnNuevoPTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoPTActionPerformed
        limpiarPapelesTrabajo();
        new MantPapelTrabajo(plantillaNueva, ventanaPrincipal).setVisible(true);
        buscarPapelesTrabajo();
}//GEN-LAST:event_btnNuevoPTActionPerformed

    private void btnAgregarPTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarPTActionPerformed
        limpiarPapelesTrabajo();
        new MantPapelTrabajo(plantillaNueva, ventanaPrincipal).setVisible(true);
        buscarPapelesTrabajo();
    }//GEN-LAST:event_btnAgregarPTActionPerformed

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
       String msjError = validarCampos();
       if (modoVentana == Principal.MODO_CREACION){
            if (msjError.isEmpty()){
                colorearTexto();
                boolean ok = crearPlantilla();
                if (ok) {
                    //bloquearPestaña(false);
                    setearModo(Principal.MODO_EDICION);
                    JOptionPane.showMessageDialog(this, "La nueva plantilla ha sido creada", "Confirmación", JOptionPane.INFORMATION_MESSAGE);
                }
                 else {
                    JOptionPane.showMessageDialog(this, "Ha ocurrido un error en la grabación. \nPor favor, intente nuevamente.", "Error", JOptionPane.ERROR_MESSAGE);
                 }
            } else {
                JOptionPane.showMessageDialog(this,"<html>" + msjError, "Error en los campos", JOptionPane.ERROR_MESSAGE);
            }
       }
       else{//modo edicion
            actualizarPlantilla();            
        }
        
    }//GEN-LAST:event_btnGuardarActionPerformed

    private boolean crearPlantilla(){
        plantillaNueva = new PlantillaMarcoReferencia();
        if (!actualizarCampos().isEmpty()) return false;
        return plantillaMRBl.guardar(plantillaNueva);
    }
    private void btnEditarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarActionPerformed
        setearModo(Principal.MODO_EDICION);
    }//GEN-LAST:event_btnEditarActionPerformed

    private void btnLimpiar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimpiar1ActionPerformed
        limpiarPapelesTrabajo();
    }//GEN-LAST:event_btnLimpiar1ActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        buscarPapelesTrabajo();
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnEditarPTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditarPTActionPerformed
        if (tblPT.getSelectedRow()==-1){
            JOptionPane.showMessageDialog(this, "Seleccione el Papel de Trabajo a editar", "Error", JOptionPane.ERROR_MESSAGE);           
            return;
        }
        PlantillaPapelTrabajo papelTrabajo = getPTdeTabla();
        //papelTrabajo.setIdPlantillaPt();
        new MantPapelTrabajo(papelTrabajo, Principal.MODO_EDICION, ventanaPrincipal).setVisible(true);
        buscarPapelesTrabajo();
    }//GEN-LAST:event_btnEditarPTActionPerformed

    private void btnEliminarPTActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarPTActionPerformed
        if (tblPT.getSelectedRow()==-1){
            JOptionPane.showMessageDialog(this, "Seleccione el Papel de Trabajo a eliminar", "Error", JOptionPane.ERROR_MESSAGE);           
            return;
        }
        int idPapelTrabajo = getIdPapelTrabajoSeleccionado();
        int opcion = JOptionPane.showConfirmDialog(this, "Está seguro que desea eliminar el papel de trabajo seleccionado?", "Error", JOptionPane.ERROR_MESSAGE);
        if (opcion == JOptionPane.YES_OPTION){
            boolean eliminado = papelTrabajoBl.eliminarPorId(idPapelTrabajo);
            if (eliminado) {
                JOptionPane.showMessageDialog(this, "El papel de trabajo ha sido eliminado", "Confirmación", JOptionPane.INFORMATION_MESSAGE);
                buscarPapelesTrabajo();
            }
            else JOptionPane.showMessageDialog(this, "Ha ocurrido un error en la eliminación. \nPor favor, intente nuevamente. ", "Error", JOptionPane.ERROR_MESSAGE);
        }        
    }//GEN-LAST:event_btnEliminarPTActionPerformed

    private void tblPTMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblPTMousePressed
        if (evt.getClickCount()>1){
            leerPapelTrabajo();
        }
    }//GEN-LAST:event_tblPTMousePressed

private void btnRefrescarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefrescarActionPerformed
    buscarPapelesTrabajo();
}//GEN-LAST:event_btnRefrescarActionPerformed

    private void limpiar(){
        txtAreaEspecializacion.setText("");
        txtDescripcion.setText("");
        txtMarcoReferencia.setText("");
        txtNivel1.setText("");
        txtNivel2.setText("");
        txtNivel3.setText("");
        txtNombre.setText("");
        txtObjetivo.setText("");
        txtPlantilla.setText("");
        colorearTexto();
        if (modoVentana == Principal.MODO_CREACION){
            plantillaNueva = null;
        }
    }

    private void colorearTexto(){
        txtAreaEspecializacion.setForeground(Color.black);
        txtDescripcion.setForeground(Color.black);
        txtMarcoReferencia.setForeground(Color.black);
        txtNivel1.setForeground(Color.black);
        txtNivel2.setForeground(Color.black);
        txtNivel3.setForeground(Color.black);
        txtNombre.setForeground(Color.black);
        txtObjetivo.setForeground(Color.black);
        txtPlantilla.setForeground(Color.black);
    }

    private void limpiarTodo() {
        txtAreaEspecializacion.setText("");
        txtDescripcion.setText("");
        txtMarcoReferencia.setText("");
        txtNivel1.setText("");
        txtNivel1PT.setText("");
        txtNivel2.setText("");
        txtNivel2PT.setText("");
        txtNivel3.setText("");
        txtNivel3PT.setText("");
        txtNombre.setText("");
        txtObjetivo.setText("");
        txtPlantilla.setText("");        
        lblNivel1.setText("{Nivel 1}:");
        lblNivel2.setText("{Nivel 2}:");
        lblNivel3.setText("{Nivel 3}:");
    }

    private void mostrarMantenimientoPlantillas() {
        ventanaPrincipal.panelPlantillas.mantenimientoPlantillas.setVisible(true);
        limpiarTodo();        
        ventanaPrincipal.panelPlantillas.remove(this);
    }

    private void llenarCmbTipo(){
        cmbTipo.addItem("-");
        for (String s: PlantillaPapelTrabajo.TIPOS_PT){
            cmbTipo.addItem(s);
        }
    }

    private String validarCampos() {

        /* completitud */
        String msjError = "";        
        if (txtNombre.getText().trim().isEmpty())
            msjError = "El campo obligatorio Nombre no ha sido completado.\n";
        else {
            if (modoVentana == Principal.MODO_CREACION){
                boolean existe = plantillaMRBl.buscarPorNombre(txtNombre.getText().trim());
                if (existe)
                    msjError += "Ya existe una plantilla con ese nombre.\n";
            }
        }
        /* campos válidos */
        String msjValidez = "";
        if (!Utils.validarAlfaNumerico(txtNombre.getText())){
            msjValidez += "Nombre, ";
            txtNombre.setForeground(Color.red);
        }
        if (!Utils.validarAlfaNumerico(txtDescripcion.getText())){
            msjValidez += "Descripción, ";
            txtDescripcion.setForeground(Color.red);
        }
        if (!Utils.validarAlfaNumerico(txtMarcoReferencia.getText())){
            msjValidez += "Marco de Referencia, ";
            txtMarcoReferencia.setForeground(Color.red);
        }
        if (!Utils.validarAlfaNumerico(txtAreaEspecializacion.getText())){
            msjValidez += "Area de Especialización, ";
            txtAreaEspecializacion.setForeground(Color.red);
        }
        if (!Utils.validarAlfaNumerico(txtObjetivo.getText())){
            msjValidez += "Objetivo, ";
            txtObjetivo.setForeground(Color.red);
        }
        if (!Utils.validarAlfaNumerico(txtNivel1.getText())){
            msjValidez += "Nivel 1, ";
            txtNivel1.setForeground(Color.red);
        }
        if (!Utils.validarAlfaNumerico(txtNivel2.getText())){
            msjValidez += "Nivel 2, ";
            txtNivel2.setForeground(Color.red);
        }
        if (!Utils.validarAlfaNumerico(txtNivel3.getText())){
            msjValidez += "Nivel 3, ";
            txtNivel3.setForeground(Color.red);
        }

        if (!msjValidez.isEmpty()){
            if (msjValidez.indexOf(',')!= msjValidez.lastIndexOf(','))
                msjValidez= "Los campos " + msjValidez + " contienen caracteres ilegales.";
            else msjValidez= "El campo " + msjValidez +  " contiene caracteres ilegales.";
            if (!msjValidez.isEmpty()) msjValidez+= " recuerde que sólo se permiten caracteres alfanuméricos, espacios en blanco y . - , ( ). ";
        }
        
        /* niveles*/
        if ((!txtNivel2.getText().isEmpty() && txtNivel1.getText().isEmpty())
                || (!txtNivel3.getText().isEmpty() && (txtNivel1.getText().isEmpty()|| txtNivel2.getText().isEmpty()))){
            msjError += "Los niveles de composición deben llenarse en orden.\n";
        }
        return msjError + msjValidez;
    }

    private void ocultarIdTabla(){
        tblPT.getColumn("Id").setWidth(0);
        tblPT.getColumn("Id").setMinWidth(0);
        tblPT.getColumn("Id").setMaxWidth(0);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregarPT;
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEditarPT;
    private javax.swing.JButton btnEliminarPT;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JButton btnLimpiar1;
    private javax.swing.JButton btnNuevoPT;
    private javax.swing.JButton btnRefrescar;
    private javax.swing.JComboBox cmbTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblComposicion;
    private javax.swing.JLabel lblNivel1;
    private javax.swing.JLabel lblNivel2;
    private javax.swing.JLabel lblNivel3;
    private javax.swing.JPanel panelPapelesTrabajo;
    private javax.swing.JTabbedPane tabPapelesTrabajo;
    private javax.swing.JTable tblPT;
    private javax.swing.JTextField txtAreaEspecializacion;
    private javax.swing.JTextArea txtDescripcion;
    private javax.swing.JTextField txtMarcoReferencia;
    private javax.swing.JTextField txtNivel1;
    private javax.swing.JTextField txtNivel1PT;
    private javax.swing.JTextField txtNivel2;
    private javax.swing.JTextField txtNivel2PT;
    private javax.swing.JTextField txtNivel3;
    private javax.swing.JTextField txtNivel3PT;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtObjetivo;
    private javax.swing.JTextField txtPlantilla;
    // End of variables declaration//GEN-END:variables

    private void buscarPapelPorIdMarco(){
        List papelesTrabajo = papelTrabajoBl.buscarPorIdMarco(plantillaNueva.getIdPlantilla());
        llenarTablaPTs(papelesTrabajo);
    }

    private void buscarPapelesTrabajo() {
        PlantillaPapelTrabajo plantillaAux = new PlantillaPapelTrabajo();
        plantillaAux.setPlantillaMarcoReferencia(plantillaNueva);
        plantillaAux.setNombre(txtPlantilla.getText().trim());
        plantillaAux.setNivel1(txtNivel1PT.getText().trim());
        plantillaAux.setNivel2(txtNivel2PT.getText().trim());
        plantillaAux.setNivel3(txtNivel3PT.getText().trim());
        plantillaAux.setTipo(cmbTipo.getSelectedIndex()-1);
        List papelesTrabajo = papelTrabajoBl.buscarPorCriterios(plantillaAux);
        for (Object o: papelesTrabajo){
            PlantillaPapelTrabajo p = (PlantillaPapelTrabajo)o;
            System.out.println("Plantilla:"+ p.getPlantillaMarcoReferencia().getIdPlantilla());
        }
        llenarTablaPTs(papelesTrabajo);
    }

    private void limpiarPapelesTrabajo() {
        txtNivel1PT.setText("");
        txtNivel2PT.setText("");
        txtNivel3PT.setText("");
        txtPlantilla.setText("");
        cmbTipo.setSelectedIndex(0);
        limpiarTablaPapelesTrabajo();
    }

    private void limpiarTablaPapelesTrabajo() {
        tblPT.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
            },
            new String [] {
                "Id", "Nombre", "Nivel 1", "Nivel 2", "Nivel 3"
            }
          
        ));
        ocultarIdTabla();
    }

        private PlantillaPapelTrabajo getPTdeTabla() {        
        int idPlantilla =  getIdPapelTrabajoSeleccionado();
        PlantillaPapelTrabajo p = papelTrabajoBl.buscarPorId(idPlantilla);
        p.setIdPlantillaPt(idPlantilla);
        return p;
    }

    void setMarcoRef(PlantillaMarcoReferencia marcoReferenciaSeleccionado, int modo) {
        plantillaNueva = marcoReferenciaSeleccionado;
        setearModo(modo);
    }

    private int getIdPapelTrabajoSeleccionado() {
        return Integer.parseInt(tblPT.getValueAt(tblPT.getSelectedRow(), 0).toString());        
    }

    private boolean actualizarPlantilla() {
        String s = actualizarCampos();
        if (s.isEmpty()){
            if (plantillaMRBl.guardar(plantillaNueva)){
                JOptionPane.showMessageDialog(this, "La plantilla ha sido actualizada", "Confirmación", JOptionPane.INFORMATION_MESSAGE);
            }
             else{
                JOptionPane.showMessageDialog(this, "Ha ocurrido un error en la actualización. \nPor favor, intente nuevamente.", "Error", JOptionPane.ERROR_MESSAGE);
             }
        }
        return false;
    }

    private String actualizarCampos() {
        String msjError = validarCampos();
        if (msjError.isEmpty()){
            if (modoVentana == Principal.MODO_EDICION){
                if (!plantillaNueva.getNivel1Composicion().isEmpty()&&txtNivel1.getText().trim().isEmpty()){
                    JOptionPane.showMessageDialog(this, "Debe completar todos los niveles previamente existentes", msjError, WIDTH);    
                    return "e1";
                }                
                if (!plantillaNueva.getNivel1Composicion().isEmpty()&&txtNivel2.getText().trim().isEmpty()){
                    JOptionPane.showMessageDialog(this, "Debe completar todos los niveles previamente existentes", msjError, WIDTH);
                    return "e2";
                }
                if (!plantillaNueva.getNivel1Composicion().isEmpty()&&txtNivel3.getText().trim().isEmpty()){
                    JOptionPane.showMessageDialog(this, "Debe completar todos los niveles previamente existentes", msjError, WIDTH);
                    return "e3";
                }
            }
            plantillaNueva.setNivel1Composicion(txtNivel1.getText().trim());
            plantillaNueva.setNivel2Composicion(txtNivel2.getText().trim());
            plantillaNueva.setNivel3Composicion(txtNivel3.getText().trim());
            colorearTexto();
            plantillaNueva.setNombre(txtNombre.getText());
            plantillaNueva.setNombreMarcoReferencia(txtMarcoReferencia.getText());
            plantillaNueva.setObjetivo(txtObjetivo.getText());
            plantillaNueva.setAreaEspecializacion(txtAreaEspecializacion.getText());
            plantillaNueva.setDescripcion(txtDescripcion.getText().trim());
        }
         else{
            JOptionPane.showMessageDialog(this,"<html>" + msjError, "Error en los campos", JOptionPane.ERROR_MESSAGE);
         }
         return msjError;
    }

    private void llenarTablaPTs(List papelesTrabajo) {
        Vector<String> tableHeaders = new Vector<String>();
        Vector tableData = new Vector();
        tableHeaders.add("Id");
        tableHeaders.add("Nombre");
        tableHeaders.add("Nivel 1");
        tableHeaders.add("Nivel 2");
        tableHeaders.add("Nivel 3");

        for(Object o : papelesTrabajo) {
            PlantillaPapelTrabajo plantilla = (PlantillaPapelTrabajo)o;
            Vector<Object> fila = new Vector<Object>();
            fila.add(plantilla.getIdPlantillaPt());
            fila.add(plantilla.getNombre());
            fila.add(plantilla.getNivel1());
            fila.add(plantilla.getNivel2());
            fila.add(plantilla.getNivel3());
            tableData.add(fila);
        }

        tblPT.setModel(new DefaultTableModel(tableData, tableHeaders));
        ocultarIdTabla();
    }

    private void leerPapelTrabajo() {
        PlantillaPapelTrabajo papelTrabajo = getPTdeTabla();
        //papelTrabajo.setIdPlantillaPt();
        new MantPapelTrabajo(papelTrabajo, Principal.MODO_LECTURA, ventanaPrincipal).setVisible(true);
        buscarPapelesTrabajo();
    }

    private void revisarNivelesComp() {
       if (plantillaNueva.getNivel1Composicion().trim().isEmpty()){
            lblNivel1.setText("{Nivel 1}:");
            lblNivel2.setText("{Nivel 2}:");
            lblNivel3.setText("{Nivel 3}:");
            txtNivel1PT.setEnabled(false);
            txtNivel2PT.setEnabled(false);
            txtNivel3PT.setEnabled(false);
            System.out.println("3 niveles");
        }
       else if(plantillaNueva.getNivel2Composicion().trim().isEmpty())
       {
            lblNivel2.setText("{Nivel 2}:");
            lblNivel3.setText("{Nivel 3}:");
            txtNivel2PT.setEnabled(false);
            txtNivel3PT.setEnabled(false);
            System.out.println("2 niveles");
        }
        else if (plantillaNueva.getNivel3Composicion().trim().trim().isEmpty()){
            lblNivel3.setText("{Nivel 3}:");
            txtNivel3PT.setEnabled(false);
            System.out.println("ultimo nivel");
        }
    }

    private void bloquearPorPermisos() {               
        //MARCO DE REFERENCIA
        if (ventanaPrincipal.getPermisos().getPermisoMarco().indexOf(UtilUsuarios.VER)>-1){
            btnEditar.setVisible(false);
            btnEditar.setEnabled(false);
            btnGuardar.setVisible(false);
            btnGuardar.setEnabled(false);
        }        
        if (ventanaPrincipal.getPermisos().getPermisoMarco().indexOf(UtilUsuarios.CREAR)>-1){
            btnEditar.setVisible(false);
            btnEditar.setEnabled(false);
            btnGuardar.setVisible(true);
            btnGuardar.setEnabled(true);
        }                
        if (ventanaPrincipal.getPermisos().getPermisoMarco().indexOf(UtilUsuarios.EDITAR)>-1){
            btnEditar.setVisible(true);
            btnEditar.setEnabled(true);
            btnGuardar.setVisible(true);
            btnGuardar.setEnabled(true);
            
        }
        //PAPEL DE TRABAJO
        if (ventanaPrincipal.getPermisos().getPermisoPapel().indexOf(UtilUsuarios.VER)>-1){
            btnEditarPT.setVisible(false);
            btnEditarPT.setEnabled(false);
            btnGuardar.setVisible(false);
            btnGuardar.setEnabled(false);
            btnEliminarPT.setVisible(false);
            btnEliminarPT.setEnabled(false);
            btnNuevoPT.setVisible(false);
            btnNuevoPT.setEnabled(false);
            btnAgregarPT.setVisible(false);
            btnAgregarPT.setEnabled(false);
        }        
        if (ventanaPrincipal.getPermisos().getPermisoPapel().indexOf(UtilUsuarios.CREAR)>-1){
            btnEditar.setVisible(false);
            btnEditar.setEnabled(false);
            btnGuardar.setVisible(true);
            btnGuardar.setEnabled(true);
            btnEliminarPT.setVisible(true);
            btnEliminarPT.setEnabled(true);
            btnNuevoPT.setVisible(true);
            btnNuevoPT.setEnabled(true);
            btnAgregarPT.setVisible(true);
            btnAgregarPT.setEnabled(true);
        }                
        if (ventanaPrincipal.getPermisos().getPermisoPapel().indexOf(UtilUsuarios.EDITAR)>-1){
            btnEditar.setVisible(true);
            btnEditar.setEnabled(true);
            btnGuardar.setVisible(true);
            btnGuardar.setEnabled(true);
            btnEliminarPT.setVisible(true);
            btnEliminarPT.setEnabled(true);
            btnNuevoPT.setVisible(true);
            btnNuevoPT.setEnabled(true);
            btnAgregarPT.setVisible(true);
            btnAgregarPT.setEnabled(true);
        } 
    }       



    

}
